﻿using Aribaccio.Device;
using Aribaccio;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BDPAutodetect.Device;

namespace BDPAutodetect.Mgrcore
{
    /// <summary>
    /// 客户机端检测设备通讯
    /// </summary>
    public class DetectDeviceCustomer : DetectDeviceBase
    {
        /// <summary>
        /// 设备通讯模块辅助
        /// </summary>
        protected DeviceSerialAscii<IDevicePara> DeviceItem { get; set; }
        /// <summary>
        /// 是否打开
        /// </summary>
        public override bool IsOpen => this.DeviceItem?.IsOpen ?? false;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="argStationType">工位类型</param>
        /// <param name="argDevice">设备连接对象</param>
        public DetectDeviceCustomer(DeviceStationType argStationType, DeviceSerialAscii<IDevicePara> argDevice)
            : base($"客户机端工位{argStationType.ToInt()}设备通讯", DeviceType.Customer, argStationType)
        {
            this.DeviceItem = argDevice;
        }

        /// <summary>
        /// 读取设备数据
        /// </summary>
        /// <param name="argInputCommand">输入指令</param>
        /// <param name="argOutputCommand">输出指令</param>
        /// <param name="argIsWaitResult">是否等待结果</param>
        /// <returns></returns>
        public ResultModel<AsciiSlaveData> ReadDeviceData(string argInputCommand, string argOutputCommand, bool argIsWaitResult)
        {
            if (this.DeviceItem == null || !this.DeviceItem.IsOpen) return new ResultModel<AsciiSlaveData>(ResultStatus.Failure);
            //定义参数
            AsciiSlaveData inputSlaveData = new AsciiSlaveData(argInputCommand, argIsWaitResult);
            AsciiSlaveData outputSlaveData = new AsciiSlaveData(argOutputCommand, argIsWaitResult);
            //获取，并返回
            return this.DeviceItem.ReadAsciiData(inputSlaveData, outputSlaveData);
        }
    }
}
